package com.ybkj.o2o.zhuanche.controller.promote;


import com.ybkj.o2o.zhuanche.common.DataGrid;
import com.ybkj.o2o.zhuanche.common.query.CarTypeCompanyQuery;
import com.ybkj.o2o.zhuanche.common.query.CarTypeQuery;
import com.ybkj.o2o.zhuanche.controller.v4.ErrorConstantV4;
import com.ybkj.o2o.zhuanche.controller.v4.ResultTransport;
import com.ybkj.o2o.zhuanche.entity.CarType;
import com.ybkj.o2o.zhuanche.entity.CarTypeCompany;
import com.ybkj.o2o.zhuanche.entity.CarTypeDetail;
import com.ybkj.o2o.zhuanche.entity.CarTypeRel;
import com.ybkj.o2o.zhuanche.entity.ServiceType;
import com.ybkj.o2o.zhuanche.manager.CarTypeCompanyManager;
import com.ybkj.o2o.zhuanche.manager.CarTypeManager;
import com.ybkj.o2o.zhuanche.manager.OrderManager;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("CarTypeResv4up")
@RequestMapping("api/orders/v4up")
public class CarTypeRes {

  @Autowired
  private CarTypeManager carTypeManager;
  
  @Autowired
  private OrderManager orderManager;

  @Autowired
  private CarTypeCompanyManager carTypeCompanyManager;

  /**
   * 创建汽车类型.
   * @param typeName 车型名称
   * @param sequence 排序号
   * @param picture 图片
   * @param appKey 系统key
   * @return 车型
   */
  
  @ResponseBody
  @RequestMapping(value = "createCarType", method = RequestMethod.POST)
  public ResultTransport createCarType(
      @RequestParam("typeName") String typeName,
      @RequestParam("sequence") Integer sequence,
      @RequestParam(required = false, value = "accessibleType") String accessibleType,
      @RequestParam(required = false, value = "ifTaxi") String ifTaxi,
      @RequestParam(required = false, value = "picture") String picture,
      @RequestParam("appKey") String appKey) {
    if (sequence == null || StringUtils.isBlank(typeName) || StringUtils.isBlank(appKey)) {
      return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
    }
    try {
      Long total = carTypeManager.countByCompanyIdV4up(null, null, appKey);
      if (total >= 30) { //天敖定制需求由10改为30
        return ResultTransport.getErrorBy(ErrorConstantV4.ZHUAN_CARTYPE_TOOMUCH);
      }
      CarTypeQuery carTypeQuery = new CarTypeQuery();
      carTypeQuery.setTypeName(typeName);
      carTypeQuery.setAppKey(appKey);
      
      Long count = carTypeManager.countByParams(carTypeQuery);
      
      if (!count.equals(0L)) {
        return ResultTransport.getErrorBy(ErrorConstantV4.ZHUAN_CARTYPE_EXISTS);
      }
      CarType carType = new CarType();
      carType.setTypeName(typeName);
      carType.setAppKey(appKey);
      carType.setPicture(picture);
      if (StringUtils.isNotBlank(picture)) {
        int index =
            Integer.parseInt(picture.substring(picture.lastIndexOf("picture"))
                .replace("picture", "").replace(".png", ""));
        String photo =
            "/assets/img/zhuanchecartype/xuanzhong" + index + ".png"
                + ",/assets/img/zhuanchecartype/weixuan" + index + ".png";
        carType.setPhoto(photo);
      }
      carType.setSequence(sequence);
      carType.setIfTaxi(ifTaxi);
      carType = carTypeManager.save(carType);
      if(StringUtils.isNotBlank(accessibleType)){
        //申明类型关联信息
        CarTypeRel carTypeRel  = new CarTypeRel();
        String accessible[];
    	  accessible = accessibleType.split(",");
    	  for (int i = 0; i < accessible.length; i++) {
    		  //循环组装保存类型关联信息
    		  carTypeRel.setCarTypeId(carType.getId());
    		  carTypeRel.setAccessibleType(accessible[i]);
    		  carTypeRel.setAppKey(appKey);
    		  carTypeManager.saveCarTypeRel(carTypeRel);
    	  }
      }
      ResultTransport transport = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      transport.setData(carType);
      return transport;
    } catch (Exception ex) {
      ex.printStackTrace();
      return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
    }
  }

  /**
   * 后台修改车型及业务分类的收费标准.
   */
  
  @ResponseBody
  @RequestMapping(value = "updateCarType", method = RequestMethod.POST)
  public ResultTransport updateCarType(
      @RequestParam("carTypeId") Long carTypeId,
      @RequestParam(required = false, value = "typeName") String typeName, 
      @RequestParam(required = false, value = "sequence") Integer sequence,
      @RequestParam(required = false, value = "picture") String picture, 
      @RequestParam(required = false, value = "accessibleType") String accessibleType,
      @RequestParam(required = false, value = "ifTaxi") String ifTaxi,
      @RequestParam("appKey") String appKey) {
    if (StringUtils.isBlank(appKey) || carTypeId == null) {
      return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
    }
    try {
      CarTypeQuery carTypeQuery = new CarTypeQuery();
      carTypeQuery.setTypeName(typeName);
      carTypeQuery.setAppKey(appKey);
      List<CarType> list = carTypeManager.findByParam(carTypeQuery);
      if (!CollectionUtils.isEmpty(list)) {
        for (CarType carType : list) {
          if (!carTypeId .equals(carType.getId())) {
            return ResultTransport.getErrorBy(ErrorConstantV4.ZHUAN_CARTYPE_EXISTS);
          }
        }
      }
      
      CarType type = carTypeManager.updateCarType(carTypeId, typeName, sequence, picture, appKey,ifTaxi,accessibleType);
      ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      instance.setData(type);
      return instance;
    } catch (Exception ex) {
      ex.printStackTrace();
      return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
    }
  }

  /**
   * 删除车型.
   */
  @ResponseBody
  @RequestMapping(value = "deleteCarType", method = RequestMethod.POST)
  public ResultTransport deleteCarType(
      @RequestParam("carTypeId") Long carTypeId,
      @RequestParam("appKey") String appKey) {
    try {
      Long total = orderManager.countRunningOrder(carTypeId, appKey);
      if (total.equals(0L)) {
        carTypeManager.deleteCarType(carTypeId, appKey);
        ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
        return instance;
      } else {
        return ResultTransport.getErrorBy(ErrorConstantV4.ZHUAN_TYPE_USING);
      }
    } catch (Exception ex) {
      ex.printStackTrace();
      return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
    }
  }

  /**
   * 后台查询车型.
   */
  @ResponseBody
  @RequestMapping(value = "queryCarTypeV4up", method = RequestMethod.GET)
  public ResultTransport queryCartypeV4up(
      @RequestParam(required = false, value = "page") Integer page,
      @RequestParam(required = false, value = "rows") Integer rows,
      @RequestParam("appKey") String appKey) {
    if (StringUtils.isBlank(appKey)) {
      return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
    }
    
    CarTypeQuery carTypeQuery = new CarTypeQuery();
    carTypeQuery.setPage(page);
    carTypeQuery.setRows(rows);
    carTypeQuery.setAppKey(appKey);
    
    try {
      DataGrid<CarType> dataGrid = carTypeManager.queryCarTypeV4up(carTypeQuery);

      ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      instance.setData(dataGrid);
      return instance;
    } catch (Exception ex) {
      ex.printStackTrace();
      return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
    }
  }

  /**
   * 查询车型图片价格配置信息.
   */
  @ResponseBody
  @RequestMapping(value = "queryCarTypeDetails", method = RequestMethod.GET)
  public ResultTransport queryCarTypeDetails(
      @RequestParam("appKey") String appKey) {
    if (StringUtils.isBlank(appKey)) {
      return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
    }
    
    try {
      List<CarTypeDetail> list = carTypeManager.queryCarTypeDetail(appKey);

      ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      instance.setData(list);
      return instance;
    } catch (Exception ex) {
      ex.printStackTrace();
      return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
    }
  }
  @ResponseBody
  @RequestMapping(value = "initCarTypeDetails", method = RequestMethod.GET)
  public ResultTransport initCarTypeDetails(
      @RequestParam("appKey") String appKey) {
    if (StringUtils.isBlank(appKey)) {
      return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
    }
    
    try {
      carTypeManager.initCarTypeDetail(appKey);
      
      ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      return instance;
    } catch (Exception ex) {
      ex.printStackTrace();
      return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
    }
  }
  /**
   * 修改车型详情.
   * @param id 车型详情对象id
   * @param photo 车型详情图片
   * @param priceDetail 车型收费详情
   * @param startPrice 起步价
   * @param timePrice 时间价
   * @param miliagePrice 里程价
   * @param appKey 系统key
   */
  @ResponseBody
  @RequestMapping(value = "updateCarTypeDetails", method = RequestMethod.POST)
  public ResultTransport queryCarTypeDetails(
      @RequestParam("id") Long id,
      @RequestParam("photo") String photo,
      @RequestParam("priceDetail") String priceDetail,
      @RequestParam("startPrice") String startPrice,
      @RequestParam("timePrice") String timePrice,
      @RequestParam("miliagePrice") String miliagePrice,
      @RequestParam("appKey") String appKey) {
    if (StringUtils.isBlank(appKey) || null == id) {
      return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
    }
    
    try {
      int num = carTypeManager.updateCarTypeDetail(id, photo, priceDetail, startPrice, timePrice, miliagePrice, appKey);
      
      ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      instance.setData(num);
      return instance;
    } catch (Exception ex) {
      ex.printStackTrace();
      return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
    }
  }
  
  /**
   * 查询业务类型.
   */
  @ResponseBody
  @RequestMapping(value = "queryServiceType", method = RequestMethod.GET)
  public ResultTransport queryServiceType(
      @RequestParam("carTypeId") Long carTypeId,
      @RequestParam("companyId") Long companyId,
      @RequestParam("serviceType") String serviceType,
      @RequestParam("appKey") String appKey) {
    if (StringUtils.isBlank(appKey)) {
      return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
    }
    
    CarTypeQuery carTypeQuery = new CarTypeQuery();
    carTypeQuery.setCarTypeId(carTypeId);
    carTypeQuery.setCompanyId(companyId);
    carTypeQuery.setServiceType(serviceType);
    carTypeQuery.setAppKey(appKey);
    
    try {
      ServiceType type = carTypeManager.queryServiceType(carTypeQuery);

      ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      instance.setData(type);
      return instance;
    } catch (Exception ex) {
      ex.printStackTrace();
      return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
    }
  }
  
  /**
   * 查询收费id.
   * @param typeId 专车车型的业务id
   * @return 收费id
   */
  @ResponseBody
  @RequestMapping(value = "queryServiceTypeById", method = RequestMethod.GET)
  public ResultTransport queryServiceTypeById(
      @RequestParam("typeId") Long typeId,
      @RequestParam("appKey") String appKey) {
    if (StringUtils.isBlank(appKey)) {
      return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
    }
    try {
      Long priceId = carTypeManager.queryServiceTypeById(typeId, appKey);

      ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      instance.setData(priceId);
      return instance;
    } catch (Exception ex) {
      ex.printStackTrace();
      return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
    }
  }
  
  /**
   * 根据公司查询车型.
   * @param companyId 公司id
   * @param appKey 系统key
   * @return 车型集合
   */
  @ResponseBody
  @RequestMapping(value = "queryCarTypesV4up", method = RequestMethod.GET)
  public ResultTransport queryCarTypesV4up(
      @RequestParam(required = false, value = "companyId") Long companyId, 
      @RequestParam("appKey") String appKey) {
    if (StringUtils.isBlank(appKey)) {
      return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
    }
    try {
      List<CarType> list = carTypeManager.queryCarTypesV4up(companyId, appKey);

      ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      instance.setData(list);
      return instance;
    } catch (Exception ex) {
      ex.printStackTrace();
      return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
    }
  }

  /**
   * 开通公司车型并配置收费.
   * @param carTypeId 车型Id
   * @param companyId 公司Id
   * @param companyName 公司名称
   * @param tangzuAreaId 普通收费标准id
   * @param rizuAreaId 日租收费id
   * @param banrizuAreaId 半日租收费id
   * @param jiejiAreaId 接机收费id
   * @param songjiAreaId 送机收费id
   * @param jiezhanAreaId 接站收费id
   * @param songzhanAreaId 送站收费id
   * @param tangzuAreaName 普通收费标准名称
   * @param rizuAreaName 日租收费标准名称
   * @param banrizuAreaName 半日租收费名称
   * @param jiejiAreaName 接机收费名称
   * @param songjiAreaName 送机收费名称
   * @param jiezhanAreaName 接站收费名称
   * @param songzhanAreaName 送站收费名称
   * @param appKey 区分系统
   * @return 车型与公司关联对象
   */
  @ResponseBody
  @RequestMapping(value = "saveCarTypeCompany", method = RequestMethod.POST)
  public ResultTransport saveCarTypeCompany(
      @RequestParam("carTypeId") Long carTypeId,
      @RequestParam("companyId") Long companyId,
      @RequestParam("companyName") String companyName,
      @RequestParam("tangzuAreaId") Long tangzuAreaId, 
      @RequestParam("rizuAreaId") Long rizuAreaId,
      @RequestParam("banrizuAreaId") Long banrizuAreaId,
      @RequestParam("jiejiAreaId") Long jiejiAreaId,
      @RequestParam("songjiAreaId") Long songjiAreaId,
      @RequestParam("jiezhanAreaId") Long jiezhanAreaId,
      @RequestParam("songzhanAreaId") Long songzhanAreaId,
      @RequestParam("tangzuAreaName") String tangzuAreaName,
      @RequestParam("rizuAreaName") String rizuAreaName,
      @RequestParam("banrizuAreaName") String banrizuAreaName,
      @RequestParam("jiejiAreaName") String jiejiAreaName,
      @RequestParam("songjiAreaName") String songjiAreaName,
      @RequestParam("jiezhanAreaName") String jiezhanAreaName,
      @RequestParam("songzhanAreaName") String songzhanAreaName,
      @RequestParam("appKey") String appKey) {
    if (carTypeId == null || companyId == null || StringUtils.isBlank(companyName)
        || StringUtils.isBlank(appKey)) {
      return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
    }

    try {
      CarType type = carTypeManager.findByCarTypeId(carTypeId, appKey);
      if (type == null) {
        return ResultTransport.getErrorBy(ErrorConstantV4.ZHUAN_CARTYPE_NOT_EXISTS);
      }
      CarTypeCompanyQuery carTypeCompanyQuery = new CarTypeCompanyQuery();
      carTypeCompanyQuery.setCompanyId(companyId);
      carTypeCompanyQuery.setCarTypeId(carTypeId);
      carTypeCompanyQuery.setAppKey(appKey);
      CarTypeCompany typeCompany = 
          carTypeCompanyManager.findByCompanyIdAndCarTypeId(carTypeCompanyQuery);
      if (typeCompany != null) {
        return ResultTransport.getErrorBy(ErrorConstantV4.ZHUAN_CARTYPE_COMPANY_EXISTS);
      }
      carTypeCompanyManager.create(carTypeId, companyId, companyName, type.getSequence(),
          tangzuAreaId, rizuAreaId, banrizuAreaId, jiejiAreaId, songjiAreaId, jiezhanAreaId,
          songzhanAreaId, tangzuAreaName, rizuAreaName, banrizuAreaName, jiejiAreaName,
          songjiAreaName, jiezhanAreaName, songzhanAreaName, appKey);

      ResultTransport transport = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      return transport;
    } catch (Exception ex) {
      ex.printStackTrace();
      return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
    }
  }

  /**
   * 修改公司绑定的车型信息.
   */
  @ResponseBody
  @RequestMapping(value = "updateCarTypeCompany", method = RequestMethod.POST)
  public ResultTransport updateCarTypeCompany(
      @RequestParam("carTypeId") Long carTypeId,
      @RequestParam("companyId") Long companyId,
      @RequestParam("companyName") String companyName,
      @RequestParam("tangzuAreaId") Long tangzuAreaId,
      @RequestParam("rizuAreaId") Long rizuAreaId,
      @RequestParam("banrizuAreaId") Long banrizuAreaId,
      @RequestParam("jiejiAreaId") Long jiejiAreaId,
      @RequestParam("songjiAreaId") Long songjiAreaId,
      @RequestParam("jiezhanAreaId") Long jiezhanAreaId,
      @RequestParam("songzhanAreaId") Long songzhanAreaId,
      @RequestParam("tangzuAreaName") String tangzuAreaName,
      @RequestParam("rizuAreaName") String rizuAreaName,
      @RequestParam("banrizuAreaName") String banrizuAreaName,
      @RequestParam("jiejiAreaName") String jiejiAreaName,
      @RequestParam("songjiAreaName") String songjiAreaName,
      @RequestParam("jiezhanAreaName") String jiezhanAreaName,
      @RequestParam("songzhanAreaName") String songzhanAreaName,
      @RequestParam("appKey") String appKey) {
    if (carTypeId == null || companyId == null || StringUtils.isBlank(companyName)
        || StringUtils.isBlank(appKey)) {
      return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
    }
      
    try {
      CarType type = carTypeManager.findByCarTypeId(carTypeId, appKey);
      if (type == null) {
        return ResultTransport.getErrorBy(ErrorConstantV4.ZHUAN_CARTYPE_NOT_EXISTS);
      }
      CarTypeCompanyQuery carTypeCompanyQuery = new CarTypeCompanyQuery();
      carTypeCompanyQuery.setCompanyId(companyId);
      carTypeCompanyQuery.setCarTypeId(carTypeId);
      carTypeCompanyQuery.setAppKey(appKey);
      CarTypeCompany typeCompany = 
          carTypeCompanyManager.findByCompanyIdAndCarTypeId(carTypeCompanyQuery);
      if (typeCompany == null) {
        return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
      }
      carTypeCompanyManager.update(carTypeId, companyId, tangzuAreaId, rizuAreaId, banrizuAreaId,
          jiejiAreaId, songjiAreaId, jiezhanAreaId, songzhanAreaId, tangzuAreaName, rizuAreaName,
          banrizuAreaName, jiejiAreaName, songjiAreaName, jiezhanAreaName, songzhanAreaName,appKey);

      ResultTransport transport = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      return transport;
    } catch (Exception ex) {
      ex.printStackTrace();
      return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
    }
  }

  /**
   * 删除车型关联的公司.
   */
  @ResponseBody
  @RequestMapping(value = "deleteCarTypeCompany", method = RequestMethod.DELETE)
  public ResultTransport deleteCarTypeCompany(
      @RequestParam("carTypeCompanyId") Long carTypeCompanyId,
      @RequestParam("appKey") String appKey) {
    try {
      carTypeCompanyManager.delete(carTypeCompanyId, appKey);
      ResultTransport transport = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      return transport;
    } catch (Exception ex) {
      ex.printStackTrace();
      return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
    }
  }

  /**
   * 查询车型下关联的公司收费信息.
   */
  @ResponseBody
  @RequestMapping(value = "queryCarTypeCompany", method = RequestMethod.GET)
  public ResultTransport queryCarTypeCompany(
      @RequestParam(required = true,  value = "carTypeId") Long carTypeId,
      @RequestParam(required = false, value = "companyId") Long companyId, 
      @RequestParam(required = false, value = "companyName") String companyName, 
      @RequestParam(required = false, value = "page") Integer page, 
      @RequestParam(required = false, value = "rows") Integer rows,
      @RequestParam("appKey") String appKey) {
    if (StringUtils.isBlank(appKey) || carTypeId == null) {
      return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
    }
    
    CarTypeCompanyQuery carTypeCompanyQuery = new CarTypeCompanyQuery();
    carTypeCompanyQuery.setCarTypeId(carTypeId);
    carTypeCompanyQuery.setCompanyId(companyId);
    carTypeCompanyQuery.setCompanyName(companyName);
    carTypeCompanyQuery.setPage(page);
    carTypeCompanyQuery.setRows(rows);
    carTypeCompanyQuery.setAppKey(appKey);
    
    try {
      
      DataGrid<CarTypeCompany> dataGrid = 
          carTypeCompanyManager.findByCarTypeId(carTypeCompanyQuery);

      ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      instance.setData(dataGrid);
      return instance;
    } catch (Exception ex) {
      ex.printStackTrace();
      return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
    }
  }
}
